//@Time:2022/4/2 19:10
//@Author:aFun

package indi.csms_springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import indi.csms_springboot.domain.*;
import indi.csms_springboot.mapper.CourseMapper;
import indi.csms_springboot.mapper.TeacherMapper;
import indi.csms_springboot.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    TeacherMapper teacherMapper;

    @Override
    public IPage<Course> findCourseByPage(String _currentPage, String _rows, Map<String, String[]> condition, Integer teaId) {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);
        Page<Course> page=new Page<>(currentPage,rows);
        QueryWrapper<Course> wrapper=new QueryWrapper<>();

        Set<String> keySet = condition.keySet();
        for (String key : keySet) {
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }
            String value = condition.get(key)[0];
            if (value != null && !"".equals(value)) {
                wrapper.like(key,value);
            }
        }
        wrapper.eq("tc.teaId",teaId);
        IPage<Course> courseByPage = teacherMapper.findCourseByPage(page, wrapper);
        return courseByPage;
    }

    @Override
    public Integer findTeaIdByAccount(User user) {
        return teacherMapper.findByAccount(user.getAccount());
    }

    @Override
    public Course findCourseByCouId(Integer couId) {
        return teacherMapper.findCourseByCouId(couId);
    }

    @Override
    public boolean addCourse(Course course) {
        int insert = courseMapper.insert(course);
        return insert>0;
    }

    @Override
    public boolean deleteCourse(Course course) {
        int i = courseMapper.deleteById(course.getCouId());
        return i>0;
    }

    @Transactional
    @Override
    public boolean deleteSelected(String[] ids) {
        for (String id : ids) {
            Course course = new Course();
            course.setCouId(Integer.parseInt(id));
            boolean b = deleteCourse(course);
            if(!b){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean updateCourse(Course course) {
        int update = courseMapper.update(course,null);
        return update>0;
    }

    @Override
    public IPage<StudentOrder> findStudentByPage(String _currentPage, String _rows, Map<String, String[]> condition, Integer teaId) {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);
        Page<Course> page=new Page<>(currentPage,rows);
        QueryWrapper<Course> wrapper=new QueryWrapper<>();
        Set<String> keySet = condition.keySet();
        for (String key : keySet) {
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }
            String value = condition.get(key)[0];
            if (value != null && !"".equals(value)) {
                if(key.equals("stuId")){
                    key="ss.stuId";
                }
                wrapper.like(key,value);
            }
        }
        wrapper.eq("tc.teaId",teaId);
        Page<StudentOrder> studentByPage = teacherMapper.findStudentByPage(page, wrapper);
        return studentByPage;
    }

    @Override
    public boolean recordScore(List<SSFTVO> SSFTVOS) {
        boolean b=false;
        for (SSFTVO SSFTVO : SSFTVOS) {
            int score = SSFTVO.getScore();
            Integer ssId = SSFTVO.getSsId();
            b = teacherMapper.recordScore(score, ssId);
        }
        return b;
    }

    @Override
    public IPage<Course> findImOpenCourse(String _currentPage, String _rows, Map<String, String[]> condition, Integer teaId) {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);
        Page<Course> page=new Page<>(currentPage,rows);
        QueryWrapper<Course> wrapper=new QueryWrapper<>();

        Set<String> keySet = condition.keySet();
        for (String key : keySet) {
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }
            String value = condition.get(key)[0];
            if (value != null && !"".equals(value)) {
                if(key.equals("couId")){
                    key="c.couId";
                }
                if(key.equals("couName")){
                    key="c.couName";
                }
                wrapper.like(key,value);
            }
        }
        wrapper.eq("isopen",2);
        wrapper.eq("judge",1);

        // 得实现根据teaId查出拥有的couId
        Collection<Integer> couIds = teacherMapper.findCouIdsByTeaId(teaId);
        wrapper.notIn("c.couId",couIds);
        wrapper.eq("tc.teaId",teaId);

        Page<Course> courseForOpen = teacherMapper.findCourseForOpen(page, wrapper);
        return courseForOpen;
    }

    @Override
    public boolean selectCourse(Course course, Teacher teacher) {
        Integer couId = course.getCouId();
        Integer teaId = teacher.getTeaId();
        boolean b = teacherMapper.selectCourse(couId, teaId);
        return b;
    }
}
